/**
 * 使用面向对象的语言实现一个计算器控制台程序，要求输入两个数和运算符，得到结果
 *
 * 使用简单工厂模式
 */

export abstract class Operation {

    private numberA: number;
    private numberB: number;

    get NumberA(): number {
        return this.numberA;
    }

    set NumberA(val: number) {
        this.numberA = val;
    }

    get NumberB(): number {
        return this.numberB;
    }

    set NumberB(val: number) {
        this.numberB = val;
    }

    public GetResult(): number | null {
        return null;
    }
}

class OperationAdd extends Operation {
    public GetResult(): number | null {
        return this.NumberA + this.NumberB;
    }
}

class OperationSub extends Operation {
    public GetResult(): number | null {
        return this.NumberA - this.NumberB;
    }
}

class OperationMul extends Operation {
    public GetResult(): number | null {
        return this.NumberA * this.NumberB;
    }
}

class OperationDiv extends Operation {
    public GetResult(): number | null {
        if (this.NumberB === 0) {
            console.log("被除数为0");
            return null;
        }
        return this.NumberA / this.NumberB;
    }
}

export class OperationFactory {
    public static CreateOperate(operate: string): Operation | null {
        let operation: Operation | null = null;
        switch (operate) {
        case "+":
            operation = new OperationAdd();
            break;
        case "-":
            operation = new OperationSub();
            break;
        case "*":
            operation = new OperationMul();
            break;
        case "/":
            operation = new OperationDiv();
            break;
        }

        return operation;
    }
}

